package com.dmap.knowledge.service.impl;

import com.dmap.knowledge.bo.company.CompanyAPPInfo;
import com.dmap.knowledge.controller.api.param.AppParam;
import com.dmap.knowledge.controller.api.param.BannerEditParam;
import com.dmap.knowledge.controller.api.param.BannerParam;
import com.dmap.knowledge.dao.BannerMapper;
import com.dmap.knowledge.po.Banner;
import com.dmap.knowledge.rpc.CompanyServiceRpc;
import com.dmap.knowledge.service.BannerService;
import com.dmap.knowledge.service.BannerTagService;
import com.dmap.knowledge.vo.BannerTagVo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Service
public class BannerServiceImpl implements BannerService {
    @Resource
    private BannerMapper bannerMapper;
    @Resource
    private BannerTagService bannerTagService;

    @Resource
    private CompanyServiceRpc companyServiceRpc;


    @Override
    public Banner getBanner(long bannerId) {
        return bannerMapper.selectByPrimaryKey(bannerId);
    }


    @Override
    public long insertBanner(BannerParam param) {
        int countBanner = 0;
        long success = 0;
        for (AppParam appParam : param.getAppList()) {
            Banner banner = new Banner();
            //rpc 调用
            CompanyAPPInfo companyAPPInfo = companyServiceRpc.selectCompanyAppInfo(appParam.getAppNumber());
            banner.setCompanyNumber(companyAPPInfo.getCompanyNumber());
            banner.setCompanyId(companyAPPInfo.getCompanyId());
            banner.setAppId(companyAPPInfo.getAppId());
            banner.setAppName(companyAPPInfo.getAppName());
            banner.setLang(param.getLang().toUpperCase());
            banner.setType(param.getType());
            banner.setImageUrl(param.getImageUrl());
            banner.setTitle(param.getTitle());
            banner.setJumpUrl(param.getJumpUrl());
            banner.setAppNumber(appParam.getAppNumber());
            banner.setAppName(appParam.getAppName());
            if (param.getArticleId() != null) {
                banner.setJumpId(Long.parseLong(param.getArticleId()));
            }
            if (param.getRecommendIndex() != 0) {
                banner.setRecommendIndex(param.getRecommendIndex());
            }
            if (param.getSort() != 0) {
                banner.setSort(param.getSort());
            }
            banner.setEndTime(param.getEndTime());
            banner.setStartTime(param.getStartTime());
            banner.setCreateTime(System.currentTimeMillis() / 1000);
            banner.setUpdateTime(System.currentTimeMillis() / 1000);
            banner.setStatus(param.getStatus());
            banner.setDescription(param.getDesc());
            if (CollectionUtils.isNotEmpty(param.getTags())) {
                success = bannerMapper.insertSelective(banner);
                if (success == 0) {
                    return 0;
                }
                int count = 0;
                for (BannerTagVo bannerTag : param.getTags()) {
                    count = count + bannerTagService.insertBannerTag(banner.getId(), bannerTag.getTagTypeId(), bannerTag.getTagTypeName(), bannerTag.getTagId(), bannerTag.getTagName(), System.currentTimeMillis() / 1000);
                }
                if (count != param.getTags().size()) {
                    return 0;
                }
            }
            countBanner += success;
        }
        if (countBanner != param.getAppList().size()) {
            return 0;
        }
        return 1;
    }

    @Override
    public int deleteBanner(long bannerIds) {
        Banner banner = bannerMapper.selectByPrimaryKey(bannerIds);
        if (banner == null) {
            return 0;
        }
        return bannerMapper.deleteByPrimaryKey(bannerIds);
    }

    @Override
    public int updateBanner(BannerEditParam param) {

        Banner bannerOne = bannerMapper.selectByPrimaryKey(Long.parseLong(param.getBannerId()));
        //如果不存在就返回失败
        if (bannerOne == null) {
            return 0;
        }
        Banner banner = new Banner();
        banner.setId(Long.parseLong(param.getBannerId()));
        if (param.getLang() != null) {
            banner.setLang(param.getLang().toUpperCase());
        }
        if (param.getType() != 0) {
            banner.setType(param.getType());
        }
        if (param.getImageUrl() != null) {
            banner.setImageUrl(param.getImageUrl());

        }
        if (param.getTitle() != null) {
            banner.setTitle(param.getTitle());
        }
        if (param.getJumpUrl() != null) {
            banner.setJumpUrl(banner.getJumpUrl());
        }
        if (param.getAppNumber() != null) {
            banner.setAppNumber(param.getAppNumber());
            CompanyAPPInfo companyAPPInfo = companyServiceRpc.selectCompanyAppInfo(param.getAppNumber());
            banner.setCompanyNumber(companyAPPInfo.getCompanyNumber());
            banner.setCompanyId(companyAPPInfo.getCompanyId());
            banner.setAppId(companyAPPInfo.getAppId());
            banner.setAppName(companyAPPInfo.getAppName());
        }
        if (param.getAppName() != null) {
            banner.setAppName(param.getAppName());
        }
        if (param.getArticleId() != null) {
            banner.setJumpId(Long.parseLong(param.getArticleId()));
        }
        if (param.getRecommendIndex() != 0) {
            banner.setRecommendIndex(param.getRecommendIndex());
        }
        if(param.getJumpUrl()!=null){
            banner.setJumpUrl(param.getJumpUrl());
        }
        if (param.getSort() != 0) {
            banner.setSort(param.getSort());
        }
        if (param.getEndTime() != 0) {
            banner.setEndTime(param.getEndTime());
        }
        if (param.getStartTime() != 0) {
            banner.setStartTime(param.getStartTime());
        }
        if (param.getStatus() != 0) {
            banner.setStatus(param.getStatus());
        }
        if (param.getDesc() != null) {
            banner.setDescription(param.getDesc());
        }
        banner.setUpdateTime(System.currentTimeMillis()/1000);
        return bannerMapper.updateByPrimaryKeySelective(banner);
    }


    public String bannerScheduleInfo(Banner banner) {
        long currentNow = System.currentTimeMillis() / 1000;
        if (banner.getStatus() == 1) {
            if (banner.getStartTime() <= currentNow) {
                if (banner.getEndTime() < currentNow) {
                    return "已结束";
                }
                return "进行中";
            } else {
                if (banner.getEndTime() >= currentNow) {
                    return "未开始";
                }
                return "时间错误";
            }
        }
        return "未上架";
    }


    @Override
    public int deleteBannerList(String bannerIds) {
        List<String> columnIdList = Arrays.asList(bannerIds.split(","));
        int count = 0;
        for (String columnId : columnIdList) {
            count += deleteBanner(Long.parseLong(columnId));
        }
        if (count == columnIdList.size()) {
            return 1;
        }
        return 0;
    }

    @Override
    public int updateBannerBrowseNumber(long bannerId) {
        Banner bannerOne = bannerMapper.selectByPrimaryKey(bannerId);
        //如果不存在就返回失败
        if (bannerOne == null) {
            return 0;
        }
        bannerOne.setBrowseNumber(bannerOne.getBrowseNumber() + 1);
        return bannerMapper.updateByPrimaryKeySelective(bannerOne);
    }
}
